package cn.qiyu5522.tk_ems.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.qiyu5522.tk_ems.entity.Resume;
import cn.qiyu5522.tk_ems.entity.dto.QueryParamDTO;
import cn.qiyu5522.tk_ems.entity.dto.ResumeDTO;
import cn.qiyu5522.tk_ems.entity.vo.ResumeVO;
import cn.qiyu5522.tk_ems.mapper.ResumeMapper;
import cn.qiyu5522.tk_ems.result.CustomMapResult;
import cn.qiyu5522.tk_ems.result.R;
import cn.qiyu5522.tk_ems.service.ResumeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Tonkey
 * @since 2023-02-25
 */
@Service
public class ResumeServiceImpl extends ServiceImpl<ResumeMapper, Resume> implements ResumeService {

    @Resource
    private ObjectMapper objectMapper;

    @Override
    public R queryResumeByPageCondition(QueryParamDTO<ResumeDTO> queryParamDTO) {
        // 查询条件
        ResumeDTO condition = queryParamDTO.getCondition();
        LambdaQueryWrapper<Resume> resumeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 分页
        IPage<Resume> resumeIPage = new Page<>(queryParamDTO.getCurrentPage(),queryParamDTO.getPageSize());
        resumeLambdaQueryWrapper
                // 根据学号查询
                .like(ObjectUtil.isNotEmpty(condition.getStudentId()),Resume::getStudentId,condition.getStudentId())
                // 学生名称 ---查询
                .like(ObjectUtil.isNotEmpty(condition.getName()),Resume::getName,condition.getName())
                // 工作城市
                .like(ObjectUtil.isNotEmpty(condition.getExpectedCity()),Resume::getExpectedCity, condition.getExpectedCity())
                // 工作类别
                .eq(ObjectUtil.isNotEmpty(condition.getExpectedCity()),Resume::getExpectedPositionCategory, condition.getExpectedPositionCategory())
                // 工作岗位名称
                .eq(ObjectUtil.isNotEmpty(condition.getExpectedPosition()),Resume::getExpectedPosition, condition.getExpectedPosition())
                // 更新时间查询-- 起始+截止
                .ge(ObjectUtil.isNotEmpty(condition.getUpdateTimeStart()),Resume::getUpdateTime,condition.getUpdateTimeStart())
                .le(ObjectUtil.isNotEmpty(condition.getUpdateTimeEnd()),Resume::getUpdateTime,condition.getUpdateTimeEnd())
                // 薪资水平
                .like(ObjectUtil.isNotEmpty(condition.getExpectedSalary()),Resume::getExpectedSalary,condition.getExpectedSalary());
        baseMapper.selectPage(resumeIPage,resumeLambdaQueryWrapper);
        // 实体类转VO
        List<ResumeVO> resumeVOS = objectMapper.convertValue(resumeIPage.getRecords(),
                new TypeReference<List<ResumeVO>>() {
        });
        Map<String, Object> map = CustomMapResult
                .packagingResult(resumeVOS, queryParamDTO.getCurrentPage(),
                        queryParamDTO.getPageSize(), resumeIPage.getTotal());
        return R.ok().data("resumeIPage",map);
    }

    @Override
    public R saveMore(List<ResumeDTO> resumeDTOs) {
        // DTO转实体
        List<Resume> resumeList = objectMapper.convertValue(resumeDTOs, new TypeReference<List<Resume>>() {
        });
        Integer integer = baseMapper.insertBatchSomeColumn(resumeList);
        return integer>0? R.ok().message("添加成功"):R.error().message("添加失败");
    }

    @Override
    public R queryResumeById(Integer id) {
        Resume resume = baseMapper.selectById(id);
        return R.ok().data("resumeInfo",resume);
    }

    @Override
    public R queryResumeByStudentId(Long id) {
        LambdaQueryWrapper<Resume> resumeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        resumeLambdaQueryWrapper.eq(Resume::getStudentId,id);
        List<Resume> resumes = baseMapper.selectList(resumeLambdaQueryWrapper);
        return CollectionUtil.isEmpty(resumes)?
                R.ok().data("flag",true).message("检测到您的简历未完善,是否编辑？")
                :R.ok().data("flag",false).data("resumeInfo",resumes.get(0));
    }
}
